package com.spsoft.workflow.common.listener;



import cn.hutool.core.convert.Convert;
import com.spsoft.core.utils.SecurityUtils;
import com.spsoft.workflow.common.cmd.BackUserTaskCmd;
import com.spsoft.workflow.common.enums.HandleProcessEnums;
import com.spsoft.workflow.entity.WfProcessLog;
import com.spsoft.workflow.service.ProcessBusinessService;
import com.spsoft.workflow.service.WfProcessLogService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ManagementService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.Date;

import static com.spsoft.workflow.common.CommonConstants.START_USER_ID;


@Component
@Slf4j
public class TaskCreatedListener implements TaskListener {
    private static final long serialVersionUID = 6305116305460351851L;
    @Resource
    private TaskService taskService;
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private WfProcessLogService processLogService;
    @Resource
    private ManagementService managementService;
    @Resource
    private ProcessBusinessService businessService;

    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("TaskCreatedListener");
        String taskDefinitionKey = delegateTask.getTaskDefinitionKey();
        if ("root".equalsIgnoreCase(taskDefinitionKey)) {
            String startUserId = Convert.toStr(delegateTask.getVariable(START_USER_ID));
            taskService.setAssignee(delegateTask.getId(), startUserId);
            log.info("TaskCreatedListener {}",taskDefinitionKey);
        } else {
            if ("100000".equals(delegateTask.getAssignee())) {
                Object autoRefuse = delegateTask.getVariable("autoRefuse");
                if (autoRefuse == null) {
                    taskService.complete(delegateTask.getId());
                    WfProcessLog processLog = new WfProcessLog();
                    processLog.setTaskId(delegateTask.getId());
                    processLog.setHandleResult(HandleProcessEnums.HANDLE_RESULT_1.getDescription());
                    processLog.setRemark("审批人为空,自动通过");
                    processLog.setCreatedBy(SecurityUtils.getUserCode());
                    processLog.setCreatedTime(new Date());
                    processLog.setProcInstId(delegateTask.getProcessInstanceId());
                    processLog.setNodeId(delegateTask.getTaskDefinitionKey());
                    processLog.setNodeName(delegateTask.getName());
                    processLog.setDeptCode(SecurityUtils.getDeptCode());
                    processLogService.save(processLog);
                    log.info("TaskCreatedListener 审批人为空,自动通过 {}",delegateTask.getId());
                } else {
                    String lastNodeCanBack = businessService.getLastNodeCanBack(delegateTask.getId());
                    managementService.executeCommand(new BackUserTaskCmd(delegateTask.getId(), lastNodeCanBack));
                    WfProcessLog processLog = new WfProcessLog();
                    processLog.setTaskId(delegateTask.getId());
                    processLog.setHandleResult(HandleProcessEnums.HANDLE_RESULT_3.getDescription());
                    processLog.setRemark("审批人为空,自动退回到上级审批节点");
                    processLog.setCreatedBy(SecurityUtils.getUserCode());
                    processLog.setCreatedTime(new Date());
                    processLog.setProcInstId(delegateTask.getProcessInstanceId());
                    processLog.setNodeId(delegateTask.getTaskDefinitionKey());
                    processLog.setNodeName(delegateTask.getName());
                    processLog.setDeptCode(SecurityUtils.getDeptCode());
                    processLogService.save(processLog);
                    log.info("TaskCreatedListener 审批人为空,自动退回到上级审批节点 {}",delegateTask.getId());
                }
            }
        }
    }
}
